#include "../lib/irrlicht/include/irrlicht.h"

using namespace irr;

static bool L_M, R_M, M_M, M_MOVE;

class MyEventReceiver : public IEventReceiver 
{

public: 
   // This is the one method that we have to implement 
   virtual bool OnEvent(const SEvent& event) 
   { 
      // Remember whether each key is down or up 
	      // mouse events
   // mouse events
   if (event.EventType == EET_MOUSE_INPUT_EVENT)
   {
      switch(event.MouseInput.Event)
      {
         //! Left mouse button was pressed down.
         case EMIE_LMOUSE_PRESSED_DOWN:// = 0,
			 {L_M=true;break;}
         //! Right mouse button was pressed down.
         case EMIE_RMOUSE_PRESSED_DOWN:
			 {R_M=true;

            break;
			 }
         //! Middle mouse button was pressed down.
         case EMIE_MMOUSE_PRESSED_DOWN:
			 {M_M=true;

            break;
			 }
         //! Left mouse button was left up.
         case EMIE_LMOUSE_LEFT_UP:
			 {L_M=false;

            break;
			 }
         //! Right mouse button was left up.
         case EMIE_RMOUSE_LEFT_UP:
			 {R_M=false;

            break;
			 }
         //! Middle mouse button was left up.
         case EMIE_MMOUSE_LEFT_UP:
			 {M_M=false;

            break;
			 }
         //! The mouse cursor changed its position.
         case EMIE_MOUSE_MOVED:
         {
#ifdef _DEBUG
            //spits out the x,y coords for the mouse position
            //helpful for designing a layout or sizing Gui items

    /*        irr::core::position2d<s32> Coords = device->getCursorControl()->getPosition();
            irr::core::stringw Caption ("Mouse Coords: ");

            Caption += Coords.X;
            Caption += L",";
            Caption += Coords.Y;

            device->setWindowCaption(Caption.c_str());*/

            // std::wcerr << Caption.c_str() << endl;
#endif
            break;
         }

         default:
            break;
      }
   } // end mouse events


      if (event.EventType == irr::EET_KEY_INPUT_EVENT) 
      { 
         KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown; 
      } 
      return false; 
   } 

   // This is used to check whether a key is being held down 
   virtual bool IsKeyDown(EKEY_CODE keyCode) const 
   { 
      return KeyIsDown[keyCode]; 
   } 
   virtual bool IsKeyUp(EKEY_CODE keyCode) 
   { 
      if((KeyIsDown[keyCode]) && (!KeyIsDownPrev[keyCode])) 
      { 
         KeyIsDownPrev[keyCode] = KeyIsDown[keyCode]; 
         return false; 
      } 
      if( (!KeyIsDown[keyCode]) && (KeyIsDownPrev[keyCode]) ) 
      { 
         KeyIsDownPrev[keyCode] = false; 
         return true;;//the key has been pressed and released 
      } 
      return false; 
   } 
    
   virtual bool IsKeyToggled(EKEY_CODE keyCode) 
   { 
      if(KeyIsDown[keyCode]) 
      { 
         if(!KeyIsToggled[keyCode]) 
         { 
            KeyIsToggled[keyCode] = true; 
            return true; 
         } 
         KeyIsDownPrev[keyCode] = KeyIsDown[keyCode]; 
      } 
      else 
      { 
         if(IsKeyUp(keyCode)) 
         { 
            if(KeyIsToggled[keyCode]) 
            { 
               if(KeyIsDownPrev[keyCode]) 
               { 
                  KeyIsToggled[keyCode] = false; 
                  KeyIsDownPrev[keyCode] = false; 
                  return true; 
               } 
               else 
               { 
                  return true; 
               } 
            } 
         } 
      } 
      return false; 
   } 
   virtual void clearKeyDown(EKEY_CODE keyCode) 
   { 
      KeyIsDown[keyCode] = false; 
   } 
   MyEventReceiver() 
   { 
      for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i) 
      { 
         KeyIsDown[i] = false; 
         KeyIsDownPrev[i] = false; 
         KeyIsToggled[i] = false; 
         KeyInToggle[i] = false; 
      } 
   } 

private: 
   // We use this array to store the current state of each key 
   bool KeyIsDownPrev[KEY_KEY_CODES_COUNT]; 
   bool KeyIsDown[KEY_KEY_CODES_COUNT]; 
   bool KeyIsToggled[KEY_KEY_CODES_COUNT]; 
   bool KeyInToggle[KEY_KEY_CODES_COUNT]; 
};